package mongo

import (
	"fmt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	"strings"
	"time"
)

type FlowLog struct {
	bytes_count int
	dist_ip string
	app_protocol string
	src_ip string
}


type Student struct {
	StudentId string `bson:"_id"`
	Name string
	Age int
	Version int
	Habbit string
}

type SensitiveLog struct{
	SensitiveLogId bson.ObjectId `bson:"_id,omitempty"`
	//SensitiveLogId string `bson:"_id"`
	Timestamp time.Time
	File_path string
	Res string
	//detail Object
	Engine string
	//timestamp date
	//type string
	Log_id string
	Flow_id string
	App_protocol string
	Dst_ip string
	//img_list Array
	Error_flag int32
	Md5 string
	Src_ip string
	Tag_mark int32
}

var collection *mgo.Collection

func init() {
	session, err := mgo.Dial("10.42.25.15:27017")
	if err != nil {
		panic(err)
	}

	session.SetMode(mgo.Monotonic, true)
	collection = session.DB("dlp_service").C("sensitive_log")
}

func Query(src_ip string, dst_ip string) *SensitiveLog{
	filter := bson.D{{"tag_mark", 0},{"src_ip",src_ip},{"dst_ip",dst_ip}}
	// 创建一个Student变量用来接收查询的结果
	var result SensitiveLog
	err := collection.Find(filter).One(&result)

	if err != nil {
		log.Println(err)
		return nil
	}
	fmt.Printf("Found a single document: %+v\n", result)
	return &result
}

func UpdateById(id bson.ObjectId)  {
	//filter := bson.D{{"_id", id}}
	filter := bson.M{"_id": id}
	update := bson.D{{"$set",
		bson.D{
			{"tag_mark", 1},
			},
	}}
	// 创建一个Student变量用来接收查询的结果
	err := collection.Update(filter, update)
	if err != nil {
		log.Fatal(err)
	}
	//fmt.Printf("Found a single document: %+v\n", &updateResult)
}

func getIp(str string) string{
	comma := strings.Index(str, ":")
	return str[0: comma]
}


//func update()  {
//	selector := bson.M{"_id": bson.ObjectIdHex("61c92be0f7d3d0133534a734")}
//	update := bson.D{
//		{"$inc", bson.D{
//			{"age", 1},
//		}},
//	}
//	err := db.C("student").Update(selector, update)
//	if err != nil {
//		panic(err)
//	}
//}

func main()  {

	results := Query("10.38.5.89", "14.215.177.38")
	fmt.Println(results)
	//UpdateById(results.SensitiveLogId)

	//s := "10.42.25.64:8990"
	//comma := strings.Index(s, ":")
	//fmt.Printf(s[0: comma])


	//tracer := "死神来了,死神byebye"
	//comma := strings.Index(tracer, ",")
	//pos := strings.Index(tracer[comma:], "死神")
	//fmt.Println(comma, pos, tracer[comma+pos:])

}
